package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.R;
import com.itheima.ydd.entity.YddLink;
import com.itheima.ydd.service.IYddLinkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@Slf4j
@RestController
@RequestMapping("/ydd_link")
public class YddLinkController {
    @Autowired
    private IYddLinkService yddLinkService;

    //分页查询
    @GetMapping("/list")
    public R list(String type, Long page, Long limit) {
        log.info("类型名称{},查询第{}页,每页数量为{}", type, page, limit);
        Page<YddLink> pageInfo = new Page<>(page, limit);
        LambdaQueryWrapper<YddLink> qw = new LambdaQueryWrapper<>();
        qw.eq(YddLink::getType, type);
        yddLinkService.page(pageInfo, qw);
        R r = R.success("count", pageInfo.getTotal());
        r.putData("totalPages", pageInfo.getPages());
        r.putData("pageSize", pageInfo.getSize());
        r.putData("currentPage", pageInfo.getCurrent());
        r.putData("data", pageInfo.getRecords());

        return r;
    }
    //回显信息
    @GetMapping("/rowInfo")
    public R rowInfo(String id) {
        YddLink yddLink = yddLinkService.getById(id);
        R r = R.success("id", yddLink.getId());
        r.putData("title", yddLink.getTitle());
        r.putData("url", yddLink.getUrl());
        r.putData("des", yddLink.getDes());
        r.putData("img", yddLink.getImg());
        r.putData("type", yddLink.getType());
        r.putData("default_data", yddLink.getDefaultData());

        return r;
    }

    //修改信息
    @PostMapping("/update")
    public R update(@RequestBody YddLink yddLink) {
        yddLinkService.updateById(yddLink);
        return R.success("","");
    }

    //删除信息
    @GetMapping("/del")
    public R deleteById(Long id) {
        yddLinkService.removeById(id);
        return R.success("","");
    }

    //添加信息
    @PostMapping("/add")
    public R add(@RequestBody YddLink yddLink) {
        yddLinkService.save(yddLink);
        return R.success("", "");
    }

}
